[% setvar title Advanced I/O (AIO) %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Advanced I/O (AIO)</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Uri Guttman &lt;<a href='mailto:uri@sysarch.com'>uri@sysarch.com</a>&gt;
  Date: 29 Sept 2000
  Mailing List: <a href='mailto:perl6-language-io@perl.org'>perl6-language-io@perl.org</a>
  Number: 350
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>This RFC describes a pragma and module which support an advanced I/O
subsystem. It is meant to be a centralized subsystem that supports a
wide range of I/O requirements, many of which are covered in other
RFCs. It doesn't add any new syntax or break any existing code.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>There are many way that coders want to do I/O in Perl and many I/O
sources as well. RFC 14 discusses ways to make open smarter and to have
handlers for each flavor. The goal is good, but it requires new syntax
and semantics for open. You have effectively multiple versions of open,
each with their own argument order. It also doesn't address asynchronous
I/O or events and callbacks. I don't see how to make a socket or http
connect and not block on the request.</p>
<p>This RFC addresses those issues in a Perl5 way by creating a new class
with attribute based methods. This allows use to add new ways to do open
and I/O and not have to learn specialized argument formats. The style is
very similar to IO:: now but more generalized and tightly integrated in
the core. This is why I choose to call it Advanced I/O (AIO).</p>
<p>The main pragma will be 'use aio' and it will allow the coder to control
and load various components. I won't have the time to specify all of
them but think along the lines of RFC 14, LWP, libnet, IO::*,
LWP::Parallel, etc. Supporting them all under this single pardigm is
simple as you don't need to know more then the minimum attributes to get
your job done. Also as I have coverd some in RFC 47, many of the
attributes will have well chosen defaults which allow for shorter
argument lists for common situations. For example, instead of</p>
<pre>	$sock = IO::Socket::INET-&gt;new(PeerAddr =&gt; 'www.perl.org',
                                          PeerPort =&gt; 'http(80)',
                                          Proto    =&gt; 'tcp');</pre>
<p>you would do:</p>
<pre>	$sock = AIO::Open( Host =&gt; 'www.perl.org',
			   Port =&gt; 80 ) ;</pre>
<p>TCP would be the default protocol as it is much more common than UDP,
and it would know it is a socket connection because of the Host/Port
attributes.</p>
<p>Similarly for LWP you would just do:</p>
<pre>	$sock = AIO::Open( Url =&gt; '<a href='http://www.perl.org' target='_blank'>www.perl.org</a>' ) ;</pre>
<p>If you refer to a special attribute, it can cause the appropriate
module to be loaded at runtime. In the above case the AIO::LWP (or
whatever it is called) would get loaded and then it would be passed the
open call.</p>
<p>One critical feature of AIO is direct support of asynchronous I/O
(including connections, server accepts, and real asynchronous file
I/O). There is no special interface required, you just specify a
callback attribute. That makes the request automatically non-blocking
and registers the event for you. You have to had previously specified an
event dispatch method (with use event or use aio) or it is a runtime
error.</p>
<p>Assuming an object $foo in package Foo, the above calls can be done with
asynchronous callbacks (see RFC 321 for more on callbacks) like this:</p>
<pre>
	$event = AIO::Open( Host =&gt; 'www.perl.org',
			    Port =&gt; 80
			    Callback =&gt; $obj  ) ;


	$event = AIO::Open( Url =&gt; '<a href='http://www.perl.org/index.html',' target='_blank'>www.perl.org</a>
			    Callback =&gt; $obj  ) ;


	Package Foo ;

	sub Connected {

		my( $self, $socket ) = @_ ;

		print &quot;i am connected to perl.org\n&quot; ;
	}

	sub Url_gotten {

		my( $self, $socket ) = @_ ;

		print $socket-&gt;read( 8096 ) ;
	}</pre>
<p>Here is an asynchronous file read :</p>
<pre>	$event = AIO::Read( Fd =&gt; $fh,
			    Count =&gt; 1000,
			    Callback =&gt; \&amp;read_handler ) ;

	sub read_handler {

		my( $read_data ) = @_ ;

		print &quot;i read [$read_data]\n&quot; ;
	}</pre>
<p>The callback method names are the default ones (we can pick better ones
but I am under deadline pressure!) or you can choose your own. Timeouts
can also be set with their own method names or defualt ones:</p>
<pre>	$event = AIO::Open( Url		=&gt; '<a href='http://www.perl.org/index.html',' target='_blank'>www.perl.org</a>
			    Callback	=&gt; $obj,
			    Method	=&gt; 'Url_Ready'
			    Timeout	=&gt; 10
			    Timeout_Method =&gt; 'URL_timeout' ) ;</pre>
<p>So you see, the same simple syntax and a consistant API lets you do
blocking and non-blocking connect, I/O and timers. No need to learn
IO::, LWP and LWP::Parallel and IO::Select. All of them are covered in
under this approach and adding new attributes is easy and won't conflict
with other code written to this specification.</p>
<p>There is much more than can be desribed and I hope to have a chance to
flesh this out but I have no more time before the October 1 deadline. I
trust you can extrapolate read/write calls and other related features
from this and RFC's 47, 321 and 345.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Implementation should be very straightforward. Much of the work is in
the event loop and I/O subsystems. Some work has to be done with
managing which module supports which attributes. Event.pm does much of
this now and its API was a major influence on this.</p>
<a name='IMPACT'></a><h1>IMPACT</h1>
<p>None. No new syntax or semantics are added. The event loop and I/O
subsystem do all the work under the hood. Translation from Perl5 will be
very simple (or not even needed)</p>
<a name='UNKNOWNS'></a><h1>UNKNOWNS</h1>
<p>None.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>Event.pm - XS based event loop module.</p>
<p>RFC 14: Modify open() to support FileObjects and Extensibility</p>
<p>RFC 47: Universal Asynchronous I/O</p>
<p>RFC 60: Safe Signals</p>
<p>RFC 174: Improved parsing and flexibility of indirect object syntax</p>
<p>RFC 321: Common Callback API for all AIO calls.</p>
<p>RFC 345: Putting an Event Loop in the Core</p>
</div>
